package com.szjlc.blog.common;

import com.szjlc.blog.common.utils.ThreadMDCUtil;
import java.util.concurrent.Callable;
import java.util.concurrent.Future;
import org.slf4j.MDC;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

/**
 * @author steve
 * @since 2022-11-28 22:26
 */

public class ThreadPoolTaskExecutorMDCWrapper extends ThreadPoolTaskExecutor {

    private static final long serialVersionUID = 3940722618853093830L;

    @Override
    public void execute(Runnable task) {
        super.execute(ThreadMDCUtil.wrap(task, MDC.getCopyOfContextMap()));
    }

    /** Callable 有返回值可抛异常 */
    @Override
    public Future<?> submit(Runnable task) {
        return super.submit(ThreadMDCUtil.wrap(task, MDC.getCopyOfContextMap()));
    }

    /** Runnable 无返回值不抛异常 */
    @Override
    public <T> Future<T> submit(Callable<T> task) {
        return super.submit(ThreadMDCUtil.wrap(task, MDC.getCopyOfContextMap()));
    }
}
